
app.controller('applydeployModalCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','$stateParams','$sce','webSocket',
     function($scope, httpLoad, $rootScope, $modal,$state, $timeout,$stateParams,$sce,webSocket) {
       $rootScope.link = '/statics/css/sortware.css';
       $rootScope.moduleTitle =$sce.trustAsHtml('应用服务 > <span>应用实例</span> > 部署');

       (function(){
         var id = $stateParams.id;
       httpLoad.loadData({
           url:'/application/detail',
           method:'GET',
           data: {id: id},
           success:function(data){
               if(data.success&&data.data){
                   $scope.supplierDetail = data.data;


               }
           }
       });
       //集群
       $scope.param={
    		   "appId":$stateParams.id,
    		   params:angular.toJson([{"param":{}}])

       }
       httpLoad.loadData({
          url: '/openshift/cluster/listOpenshiftClusterByAppId',
         method: 'POST',
         data: $scope.param,
         noParam: true,
         success: function (data) {
           if (data.success) {
             $scope.masterData = data.data;

           }
         }
       });
       })();
       $scope.goBack = function(){
           $state.go('app.adhibition.apply');
       };
     }
 ]);
app.controller('applydeployCtrl', ['$rootScope', '$scope','$state','httpLoad','$stateParams','LANGUAGE',function($rootScope, $scope,$state,httpLoad,$stateParams,LANGUAGE) {
    $scope.envList = [{key:'',value:''}];
    $scope.labelList = [{keys:'',values:''}];
    $scope.name;
    $scope.scriptItem = {}
   var id = $stateParams.id;
   var params = {
           simple: true

   }
   //应用部署镜像名称接口
    httpLoad.loadData({
           url: '/software/list',
           method: 'POST',
           data: params,
           noParam: true,
           success: function (data) {
               if (data.success) {
                   $scope.dataType = data.data.rows;
               }
           }
       });


	//添加环境方法
       $scope.addEnvs = function(){
                   $scope.envList.push({key:'',value:''})
   			}
        $scope.addLabels = function(){
           //做验证-》只有上面用户组有用户下面才可以继续添加用户组
                    $scope.labelList.push({keys:'',values:''})
         }
   			$scope.removeEnv = function(key){
   				if($scope.envList.length == 1) return $scope.pop('请至少添加一组','error');
   				$scope.envList.splice(key,1);
   			}
        $scope.removeLabel = function(key){
          if($scope.labelList.length == 1) return $scope.pop('请至少添加一组','error');
          $scope.labelList.splice(key,1);
        }


     $scope.ok = function () {
         var envList = {};
         var labelList = {};
         for(var i=0;i<$scope.envList.length;i++){
        	 var item = $scope.envList[i];
        	 if((!item.key&&item.value)||(item.key&&!item.value)){
                 $scope.pop('请添加完整的环境变量','error');
                 return;
              }else if(!item.key&&!item.value){
            	  continue
              }else{
                 envList[item.key] = item.value;
              }
         }
         for(var j=0;j<$scope.labelList.length;j++){
        	 var items = $scope.labelList[j];
        	 if((!items.keys&&items.values)||(items.keys&&!items.values)){
                 $scope.pop('请添加完整的标签','error');
                 return;
              }else if(!items.keys&&!items.values){
            	  continue
              }else{
            	  labelList[items.keys] = items.values;
              }
         }

       
       var param = {
          clusterId:$scope.clusterId,
        	   appId:id,
    		   imageId:$scope.scriptItem.selected.id,
    		   appName:$scope.name,
    		   envs:envList,
    		   labels:labelList,
    		   remark:$scope.remark
    		   };
          httpLoad.loadData({
               url: '/software/deployImage',
              method:'POST',
              data: param,
              success:function(data){
                  if(data.success){
                	  $state.go('app.adhibition.apply');
                  }
              }
          });
      }
     $scope.cancel = function(){
         $state.go('app.adhibition.apply');
     };


}]);
app.controller('applygridinCtrl', ['$rootScope', '$scope','$state','httpLoad','$stateParams','LANGUAGE','$timeout',
                                   function($rootScope, $scope,$state,httpLoad,$stateParams,LANGUAGE,$timeout) {
	var params = {
	        simple: true
	}
	$scope.codemire = function(){
		 $scope.code = true;
	}
  $scope.scriptItem = {}
	 httpLoad.loadData({
	        url: '/layout/list',
	        method: 'POST',
	        data: params,
	        noParam: true,
	        success: function (data) {
	            if (data.success) {
	                $scope.composeList = data.data.rows;
	            }
	        }
	    });
    $scope.layoutDeploy = function () {
   	 httpLoad.loadData({
 	        url: '/application/layoutDeploy',
 	        method: 'POST',
 	        data: {
             clusterId:$scope.clusterId,
            appId:$stateParams.id,
             layoutId:$scope.scriptItem.selected.id
           },

 	        success: function (data) {
 	            if (data.success) {
                	$scope.pop(data.message);
                $state.go('app.adhibition.apply');
 	            }
 	        }
 	    });
    }
	// 获取选择文件id然后通过接口获得文件内容
    $scope.codeMirror = {};
     $scope.showComposeFile = function(){
     	 httpLoad.loadData({
  	        url: '/layout/detail',
  	        method: 'GET',
  	        data: {id:$scope.scriptItem.selected.id},
  	      //  noParam: true,
  	        success: function (data) {
  	            if (data.success) {
  	            	 $scope.code = true;
  	              $scope.supplierDetail = data.data;
  	            $timeout(function () {
  	            	 $scope.codeMirror.codeMirror.setValue($scope.supplierDetail.fileContent);
  	          }, 100);
  	            }
  	        }
  	    });
     };

}]);
